package keter.service;

import java.util.List;

import keter.dao.org.AccountDao;
import keter.domain.Account;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccountService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(AccountService.class);
	
	@Autowired
	private AccountDao dao;
	
	public AccountDao getDao() {
		return dao;
	}

	
//	public void save(Account account){
//		dao.save(account);
//	}
	
	/**
	 * <p>Method ：update
	 * <p>Description : 用户修改
	 * 由于前台只允许修改部分字段，缺失内容无法通过校验，因此将有修改后的新对象字段合并到原对象中
	 * @param account
	 * @param roleName 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	public void update(Account account){	
		Account oldUser = findById(account.getId());
		oldUser.setRoleList(account.getRoleList());
		oldUser.setRemark(account.getRemark());
		dao.save(oldUser);
	}
	
	public List<Account> all(){
		logger.info("find all user");
		return dao.findAll();
	}
	
	public Account findByLogin(String login){
		return dao.findByName(login);
	}
	
	public Account findById(Long id){
		return dao.findOne(id);
	}
	
	public boolean validateUnique(String name){
	    if(dao.findByName(name)!=null){
	        return false;
	    }
	    return true;
	}
	

	
	public void delete(Long id){
		dao.delete(id);
	}

}
